<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="res/layui/css/layui.css" media="all">
    <link rel="stylesheet"
          href="res/zTree_v3-master/css/metroStyle/metroGreen.css"
          type="text/css">
    <script src="res/js/jquery-3.4.1.min.js" type="text/javascript"
            charset="utf-8"></script>
    <script type="text/javascript" src="res/layui/layui.js"></script>
    <script type="text/javascript" src="res/js/base.js"></script>
    <script type="text/javascript">

        //全局同步
        $.ajaxSetup({
            async: false
        });
    </script>
    <title>操作界面</title>
</head>
<body>
<div style="margin-top: 10px; padding-right: 27px;">
    <form class="layui-form" id="idform" action="" method="post"

          enctype="multipart/form-data">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>用户修改界面</legend>
        </fieldset>

        <div class="layui-upload" style="margin-left: 14%;">
            <button type="button" class="layui-btn layui-btn-warm" id="test1">上传图片</button>

            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" width="100" height="100">
                <p id="demoText"></p>
                <input type="hidden" name="imageOne" id="imageOne">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="userName" name="userName" lay-verify="required|title"
                       autocomplete="off" disabled placeholder="用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" id="userPassword" name="userPassword" lay-verify="required|title1"
                       autocomplete="off" placeholder="密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" id="userPassword1" name="userPassword1" lay-verify="required|title1"
                       autocomplete="off" placeholder="确认密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证</label>
            <div class="layui-input-block">
                <input type="text" id="userEmail" name="userEmail" lay-verify="required|title"
                       autocomplete="off" placeholder="身份证" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-block">
                <input type="text" id="userPhone" name="userPhone" lay-verify="phone"
                       autocomplete="off" placeholder="手机" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit
                        lay-filter="demo2">立即提交
                </button>
            </div>
        </div>
    </form>
</div>
<script>
    $(function () {
        var url = getRootPath()+"Tuser/getUs";
        var data = null;
        $.post(url,data,function(obj){
            $("#userPhone").val(obj.userPhone);
            $("#userEmail").val(obj.userEmail);
            $("#userName").val(obj.userName);
            $('#demo1').attr('src', getRootPath()+obj.userPhoto);
        },"json")
    })
    layui.use(['form', 'layedit', 'upload','layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate
            , $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: getRootPath()+'Tuser/getImg'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                $("#imageOne").val(res.src1);
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        //自定义验证规则
         form.verify({
             title1: function(value){
                 if(value != $("#userPassword1").val() || value != $("#userPassword").val()){
                     return '2次密码不一致！';
                 }
             }
             ,phone: [
                 /^1[3456789]\d{9}$/
                 ,'电话,岂能填错！'
             ]
         });
        //监听提交
        form.on('submit(demo2)', function (data) {
            var index1 = layer.load(0); //添加laoding,0-2两种方式
            var form = new FormData(document.getElementById("idform"));
            form.userId = getQueryStringOne("userId");
            $.ajax({
                url: getRootPath()+"Tuser/addOrup",
                type: "post",
                data: form,
                dataType: "json",
                processData: false,
                contentType: false,
                success: function (obj) {
                    layer.close(index1);    //返回数据关闭loading
                    if (obj.status == 1) {
                        //弹出需求
                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 1000
                            //一秒关闭 如果不配置是3秒
                        }, function () {
                            top.location.href = "login.html";
                            //刷新主界面
                            //parent.location.reload();
                        });
                    } else {
                        layer.msg(obj.msg);
                    }
                },
                error: function (e) {

                }
            });
            return false;
        });

    });

    //得到地址栏上的参数  可以 是中文
    function getQueryStringOne(key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    }
</script>
</body>
</html>